import { createRouter, createWebHistory } from "vue-router";
import { getlocalStorage } from "@/hooks/operationLocalStorage";
import addRouters from "@/hooks/addRouters";
import NProgress from "nprogress";
const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes: [
		{
			path: "",
			redirect: "/sign",
		},
		{
			path: "/sign",
			name: "sign",
			component: () => import("@/views/sign/sign.vue"),
			beforeEnter: (to, from) => {
				// ...
				if (getlocalStorage("token")) {
					router.push("/home/indexdefault");
					return false;
				}

				return true;
			},
		},
		{
			path: "/home",
			name: "home",
			component: () => import("@/views/home/index.vue"),
			children: [
				{
					path: "",
					redirect: "/home/indexdefault",
				},
				{
					path: "indexdefault",
					name: "indexindexdefault",
					component: () => import("@/views/home/indexdefault.vue"),
				},
			],
		},
	],
});

router.beforeEach((to, from, next) => {
	// console.log(to, from);
	NProgress.start();
	NProgress.inc();
	next();
});

router.afterEach((to, from) => {
	NProgress.done();
});

addRouters(router, true);

export default router;
